package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.BikePageModel;
import com.southwind.common.Cache;
import com.southwind.common.DataGridView;
import com.southwind.common.ResultObj;
import com.southwind.entity.Admin;
import com.southwind.entity.Bike;
import com.southwind.mapper.AdminMapper;
import com.southwind.mapper.AreaMapper;
import com.southwind.mapper.BikeMapper;
import com.southwind.service.BikeService;
import com.southwind.vo.BarVO;
import com.southwind.vo.BikeVO;
import com.southwind.vo.PieVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/bike")
public class BikeController {

    @Autowired
    private BikeService bikeService;
    @Autowired
    private BikeMapper bikeMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AreaMapper areaMapper;

    @RequestMapping("/list")
    @ResponseBody
    public List<Bike> list(){
        List<Bike> list = this.bikeService.list();
        return list;
    }

    @RequestMapping("/bar")
    @ResponseBody
    public Map<String,List> bar(){
        List<BarVO> barVOS = this.bikeMapper.barVO();
        List<String> areaList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        for (BarVO barVO : barVOS) {
            areaList.add(barVO.getArea());
            countList.add(barVO.getCount());
        }
        Map<String,List> map = new HashMap<>();
        map.put("areaList",areaList);
        map.put("countList",countList);
        return map;
    }

    @RequestMapping("/pie")
    @ResponseBody
    public List<PieVO> pie(){
        return this.bikeMapper.pieVO();
    }

    @GetMapping("/load")
    public DataGridView load(BikePageModel bikePageModel){
        Page<Bike> page = new Page<>(bikePageModel.getPage(), bikePageModel.getLimit());
        QueryWrapper<Bike> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(bikePageModel.getNumber()), "number", bikePageModel.getNumber());
        Page<Bike> resultPage = this.bikeService.page(page,queryWrapper);
        List<BikeVO> list = new ArrayList<>();
        for (Bike record : resultPage.getRecords()) {
            BikeVO bikeVO = new BikeVO();
            BeanUtils.copyProperties(record, bikeVO);
            bikeVO.setArea(this.areaMapper.getAreaById(record.getAid()));
            list.add(bikeVO);
        }
        DataGridView dataGridView = new DataGridView(resultPage.getTotal(), list);
        return dataGridView;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody Bike bike){
        boolean save = false;
        try {
            save = this.bikeService.save(bike);
        } catch (DuplicateKeyException e) {
            return ResultObj.BIKE_NUMBER_ERROR;
        }
        if(!save) return ResultObj.BIKE_ADD_ERROR;
        return ResultObj.BIKE_ADD_SUCCESS;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody Bike bike){
        boolean updateById = this.bikeService.updateById(bike);
        if(!updateById) return ResultObj.BIKE_UPDATE_ERROR;
        return ResultObj.BIKE_UPDATE_SUCCESS;
    }

    @RequestMapping("/delete")
    public ResultObj delete(Integer id){
        boolean removeById = this.bikeService.removeById(id);
        if(!removeById) return ResultObj.BIKE_DELETE_ERROR;
        return ResultObj.BIKE_DELETE_SUCCESS;
    }
}

